﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace CNDM.BO.GetData
{
    public class Data
    {
        public static NewsClass.Category GetAllCategory()
        {
            DataTable dt = BO.Category.CategoryData.Get_All_Category();
            NewsClass.Category category = new NewsClass.Category();
            List<NewsClass.Category.CategoryItem> items = new List<NewsClass.Category.CategoryItem>();
            string catname = "";
            string catid = "";
            string catparentid = "";
            string image = "";
            string countNews = "0";
            foreach (DataRow dr in dt.Rows)
            {
                catname = dr["Cat_name"].ToString();
                catid = dr["Cat_ID"].ToString();
                catparentid = dr["Cat_ParentID"].ToString();
                image = dr["Cat_Icon"].ToString();
                countNews = dr["CountNews"].ToString();
                if (image != "")
                {
                    image = Xmobile.Libs.Constants.DomainImage + image;
                }
                NewsClass.Category.CategoryItem item = new NewsClass.Category.CategoryItem(catname, catid, catparentid, image, countNews);
                items.Add(item);
            }

            category.CategoryItems = items.ToArray();

            return category;

        }

        public static NewsClass.CategoryAll GetCategoryList()
        {
            DataTable dtParent = BO.Category.CategoryData.Get_Category(0);
            DataTable dtChild = new DataTable();
            NewsClass.CategoryAll category = new NewsClass.CategoryAll();
            List<NewsClass.CategoryAll.CategoryList> items = new List<NewsClass.CategoryAll.CategoryList>();
            string catname = "";
            string catid = "";
            string catparentid = "0";
            string image = "", icon="";
            string countNews = "0";
            foreach (DataRow dr in dtParent.Rows)
            {
                catname = dr["Cat_name"].ToString();
                catid = dr["Cat_ID"].ToString();
                image = dr["Cat_Icon"].ToString();
                //countNews = dr["CountNews"].ToString();
                if (image != "")
                {
                    image = Xmobile.Libs.Constants.DomainImage + image;
                }
                //catparentid = dr["Cat_ParentID"].ToString();
                dtChild = BO.Category.CategoryData.Get_Category(Convert.ToInt32(catid));

                List<NewsClass.Category.CategoryItem> itemListChils = new List<NewsClass.Category.CategoryItem>();
                foreach (DataRow drChild in dtChild.Rows)
                {
                    countNews = drChild["CountNews"].ToString();
                    icon = "";
                    if (drChild["Cat_Icon"].ToString() != "")
                    {
                        icon = Xmobile.Libs.Constants.DomainImage + drChild["Cat_Icon"].ToString();
                    }
                    NewsClass.Category.CategoryItem itemChilds = new NewsClass.Category.CategoryItem(drChild["Cat_Name"].ToString(), drChild["Cat_ID"].ToString(), catid, icon, countNews);
                    itemListChils.Add(itemChilds);
                }

                NewsClass.CategoryAll.CategoryList item = new NewsClass.CategoryAll.CategoryList(catname, catid, catparentid, image, itemListChils);
                items.Add(item);
            }

            category.CategoryLists = items.ToArray();

            return category;

        }


        public static NewsClass.NewsList GetNewsList(string catID, Int32 pageindex, Int32 pagesize)
        {
            DataTable dt = BO.News.NewsData.Get_DanhSachTin("0", catID, pageindex, pagesize);
            NewsClass.NewsList newsList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsListItems = new List<NewsClass.NewsList.NewsListItem>();
            string title = "";
            string desc = "";
            string image = "";
            string link = "";
            string catid = "";
            string newsid = "";
            
            foreach (DataRow dr in dt.Rows)
            {
                title = dr["News_Title"].ToString();
                desc = dr["News_InitContent"].ToString();
               
                image = dr["Image_Url"].ToString();
                catid = dr["Cat_ID"].ToString();
                link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();
                newsid = dr["News_ID"].ToString();
                NewsClass.NewsList.NewsListItem item = new NewsClass.NewsList.NewsListItem(title, image, desc, catid, link, newsid);
                newsListItems.Add(item);
            }

            newsList.NewsItems = newsListItems.ToArray();

            return newsList;
        }

        public static NewsClass.NewsList GetTinMoiNhat(int Cat_ID, int Top)
        {
            DataTable dt = BO.News.NewsData.Get_TinMoiNhat(Cat_ID, Top);
            NewsClass.NewsList newsList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsListItems = new List<NewsClass.NewsList.NewsListItem>();
            string title = "";
            string desc = "";
            string image = "";
            string link = "";
            string catid = "";
            string newsid = "";

            foreach (DataRow dr in dt.Rows)
            {
                title = dr["News_Title"].ToString();
                desc = dr["News_InitContent"].ToString();

                image = dr["Image_Url"].ToString();
                catid = dr["Cat_ID"].ToString();
                link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();
                newsid = dr["News_ID"].ToString();
                NewsClass.NewsList.NewsListItem item = new NewsClass.NewsList.NewsListItem(title, image, desc, catid, link, newsid);
                newsListItems.Add(item);
            }

            newsList.NewsItems = newsListItems.ToArray();

            return newsList;
        }

        public static NewsClass.NewsContent GetContent(long newsid, int topOther)
        {
            DataTable dt = BO.News.NewsData.Get_NewsDetail(newsid);
            DataTable dtOther = new DataTable();
            NewsClass.NewsContent newDetail = new NewsClass.NewsContent();
            NewsClass.NewsList newsOtherList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsOtherListItems = new List<NewsClass.NewsList.NewsListItem>();

            string title = "";
            string desc = "";
            string image = "";
            string content = "";
            string link = "";
            int catid = 0;

            string _title = "";
            string _desc = "";
            string _image = "";
            string _link = "";
            string _catid = "";
            string _newsid = "";

            title = dt.Rows[0]["News_Title"].ToString();
            desc = dt.Rows[0]["News_InitContent"].ToString();
            catid = Convert.ToInt32(dt.Rows[0]["Cat_ID"]);
            image = dt.Rows[0]["Image_Url"].ToString();
            content = dt.Rows[0]["News_Content"].ToString();
            link = dt.Rows[0]["NewsUrl"].ToString();
            dtOther = News.NewsData.Get_DanhSachTinMoi(newsid, topOther, catid);
            if (dtOther == null || dtOther.Rows.Count==0)
            {
                dtOther = News.NewsData.Get_DanhSachTinCu(newsid, topOther, catid);

            }

            foreach (DataRow dr in dtOther.Rows)
            {
                _title = dr["News_Title"].ToString();
                _desc = dr["News_InitContent"].ToString();

                _image = dr["Image_Url"].ToString();
                _catid = dr["Cat_ID"].ToString();
                _link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();
                _newsid = dr["News_ID"].ToString();

                NewsClass.NewsList.NewsListItem _item = new NewsClass.NewsList.NewsListItem(_title, _image, _desc, _catid, _link, _newsid);
                newsOtherListItems.Add(_item);
            }

            newsOtherList.NewsItems = newsOtherListItems.ToArray();

            NewsClass.NewsContent.NewsContentItem item = new NewsClass.NewsContent.NewsContentItem(title, desc, content, image, link, newsOtherList);
            newDetail.NewsContentItems = item;

            return newDetail;
        }

        public static NewsClass.NewsList Search(string keysearch, int pageindex, int pagesize)
        {
            DataTable dt = BO.News.NewsData.Search_News(keysearch, pageindex, pagesize);
            NewsClass.NewsList newsList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsListItems = new List<NewsClass.NewsList.NewsListItem>();
            string title = "";
            string desc = "";
            string image = "";
            string link = "";
            string catid = "";
            string newsid = "";

            foreach (DataRow dr in dt.Rows)
            {
                title = dr["News_Title"].ToString();
                desc = dr["News_InitContent"].ToString();
                
                image = dr["Image_Url"].ToString();
                catid = dr["Cat_ID"].ToString();
                link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();
                newsid = dr["News_ID"].ToString();
                NewsClass.NewsList.NewsListItem item = new NewsClass.NewsList.NewsListItem(title, image, desc, catid, link, newsid);
                newsListItems.Add(item);
            }

            newsList.NewsItems = newsListItems.ToArray();

            return newsList;
        }

        public static NewsClass.NewsList NoiBatTrangChu(int top)
        {
            DataTable dt = BO.News.NewsData.Get_NoiBatTrangChu(top);
            NewsClass.NewsList newsList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsListItems = new List<NewsClass.NewsList.NewsListItem>();
            string title = "";
            string desc = "";
            string image = "";
            string link = "";
            string catid = "";
            string newsid = "";

            foreach (DataRow dr in dt.Rows)
            {
                title = dr["News_Title"].ToString();
                desc = dr["News_InitContent"].ToString();
               
                image = dr["Image_Url"].ToString();
                catid = dr["Cat_ID"].ToString();
                link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();
                newsid = dr["News_ID"].ToString();
                NewsClass.NewsList.NewsListItem item = new NewsClass.NewsList.NewsListItem(title, image, desc, catid, link, newsid);
                newsListItems.Add(item);
            }

            newsList.NewsItems = newsListItems.ToArray();
            return newsList;
        }

        public static NewsClass.NewsList NewsOtherList(long _newsid, int top, int _catid)
        {
            DataTable dt = new DataTable();
            dt = BO.News.NewsData.Get_DanhSachTinMoi(_newsid, top, _catid);
            NewsClass.NewsList newsList = new NewsClass.NewsList();
            List<NewsClass.NewsList.NewsListItem> newsListItems = new List<NewsClass.NewsList.NewsListItem>();
            string title = "";
            string desc = "";
            string image = "";
            string link = "";
            string catid = "";
            string newsid = "";
            if (dt == null || dt.Rows.Count==0)
            {
                dt = BO.News.NewsData.Get_DanhSachTinCu(_newsid, top, _catid);
            }

            foreach (DataRow dr in dt.Rows)
            {
                title = dr["News_Title"].ToString();
                desc = dr["News_InitContent"].ToString();
                //if (dr["Image_Url"].ToString() != "")
                //    image = Xmobile.Libs.Constants.DomainImage + dr["Image_Url"].ToString();
                catid = dr["Cat_ID"].ToString();
                if (dr["NewsUrl"].ToString() != "")
                {
                    link = Xmobile.Libs.Constants.DomainName + dr["NewsUrl"].ToString();

                }

                newsid = dr["News_ID"].ToString();
                NewsClass.NewsList.NewsListItem item = new NewsClass.NewsList.NewsListItem(title, image, desc, catid, link, newsid);
                newsListItems.Add(item);
            }

            return newsList;
        }
    }
}
